package com.base.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.base.comm.CommContent;
import com.base.po.voip.Share;
import com.base.service.LogsService;
import com.base.service.ShareService;
import com.base.utils.IP;
import com.base.utils.Produce;

/**
 * 添加分享图片服务端
 * 
 * @author
 *
 */
@Controller
@RequestMapping("/admin")
public class add_Share {

	@Autowired
	private LogsService logsService; // log日志
	@Autowired
	private ShareService shareService; // 分享页图片

	// 添加分享图片
	@RequestMapping(value = "/toAddShare", method = RequestMethod.POST)
	public @ResponseBody int toAddShare(HttpServletRequest request, Share share) throws IOException, ParseException {
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		if (userid == null || userid.equals("")) return -1; // 未登录
		List<Share> addShare = shareService.selectByAll(new Share(userid, share.getType(), (byte)1));
		if (share.getType() == 0) { // 添加分享页 只能添加三张
		    return -4; // 失败
		}
		if (share.getType() == 1) { // 添加分享页 只能添加三张
			// 获取分享页
			if (addShare != null && addShare.size() > 2) return -3; // 失败
		}
		if (share.getType() == 2 || share.getType() == 3) { // 添加注册页 只能添加一张
			System.out.println(userid);
			// 获取注册页
			if (addShare != null && addShare.size() > 0) return -2; // 失败
			
			share.setTotop(0);
			share.setToleft(0);
			share.setQrcodesize(0);
		}
		share.setId(Produce.getUUID()); // 设置参数 UUID
		share.setCreatetime(Produce.getNowDate()); // 时间
		share.setParentid(userid); // 代理商ID
		share.setDatastatus((byte) 1); // 数据状态
		logsService.insert(CommContent.TYPE_CREATE, CommContent.LOGS_XIAOXI + "配置分享页图片信息",
				(String) request.getSession().getAttribute("userNick"), true, IP.getIpAddr(request));
		return shareService.insertSelective(share); // 添加
	}
}
